﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Duality;
using Duality.Resources;
using Duality.VertexFormat;
using Duality.ColorFormat;

using OpenTK;

namespace ClientApp
{
	[Serializable]
    public sealed class PlayerHUD : Component, ICmpScreenOverlayRenderer
    {
		private	PlayerIdentity	id	= PlayerIdentity.Unknown;
		
		public PlayerIdentity PlayerId
		{
			get { return this.id; }
			set { this.id = value; }
		}

		void ICmpScreenOverlayRenderer.DrawOverlay(IDrawDevice device)
		{
			Canvas canvas = new Canvas(device);
			canvas.CurrentState.TextFont = GameRes.Data.Gfx.PlayerHUD_Font;

			this.DrawCherryGUI(canvas);
			this.DrawLemonGUI(canvas);
			this.DrawInputPaths(canvas);
		}
		bool ICmpScreenOverlayRenderer.IsVisible(IDrawDevice device) { return true; }

		public void DrawCherryGUI(Canvas canvas)
		{
            Rect screenRect = new Rect(DualityApp.TargetResolution);
			PlayerIdentity id = PlayerIdentity.Cherry;
			float hp = Player.Cherry.Hq != null ? Player.Cherry.Hq.Hitpoints : 0.0f;
			float mana = Player.Cherry.Mana;
			bool btnRock = Player.Cherry.CommandingRock;
			bool btnPaper = Player.Cherry.CommandingPaper;
			bool btnScissor = Player.Cherry.CommandingScissor;
			WarriorCommand cmdRock = Player.Cherry.CurrentCmdRock;
			WarriorCommand cmdPaper = Player.Cherry.CurrentCmdPaper;
			WarriorCommand cmdScissor = Player.Cherry.CurrentCmdScissor;
			canvas.PushState();

            canvas.CurrentState.TransformAngle = MathF.RadAngle90;

            canvas.CurrentState.SetMaterial(GameRes.Data.Gfx.emptymana_Material);
            canvas.CurrentState.TransformHandle = new Vector2(75, 200);
			canvas.DrawTexturedRect(screenRect.MinX, screenRect.MaxY);

            canvas.CurrentState.SetMaterial(GameRes.Data.Gfx.fullmana_Material);
            canvas.CurrentState.TransformHandle = new Vector2(75, 200 * mana);
			canvas.DrawTexturedRect(
				screenRect.MinX, screenRect.MaxY, 75, 200 * mana,
				0.0f, 1.0f - mana, 1.0f, mana);

            canvas.CurrentState.TransformHandle = new Vector2(0, 0);
			canvas.CurrentState.ColorTint = ColorRgba.Black;
			canvas.DrawText(
				string.Format("{0}", MathF.RoundToInt(mana * 100.0f)), 
				screenRect.MinX + 30 - 2, 
				screenRect.MaxY - 110 + 2);
			canvas.CurrentState.ColorTint = new ColorRgba(96, 164, 255);
			canvas.DrawText(
				string.Format("{0}", MathF.RoundToInt(mana * 100.0f)), 
				screenRect.MinX + 30, 
				screenRect.MaxY - 110);

            canvas.CurrentState.TransformHandle = new Vector2(0, 0);
			canvas.CurrentState.ColorTint = ColorRgba.Black;
			canvas.DrawText(
				string.Format("{0}", MathF.RoundToInt(hp * 100.0f)), 
				screenRect.MinX + 30 - 2, 
				screenRect.MaxY - 160 + 2);
			canvas.CurrentState.ColorTint = new ColorRgba(255, 64, 64);
			canvas.DrawText(
				string.Format("{0}", MathF.RoundToInt(hp * 100.0f)), 
				screenRect.MinX + 30, 
				screenRect.MaxY - 160);

			canvas.CurrentState.ColorTint = id.GetUnitColor();
			canvas.CurrentState.SetMaterial(btnRock ? GameRes.Data.Gfx.command_rock_active_Material : GameRes.Data.Gfx.command_rock_Material);
			canvas.DrawTexturedRect(screenRect.MinX + 310, screenRect.MinY + 10);
			canvas.CurrentState.SetMaterial(btnPaper ? GameRes.Data.Gfx.command_paper_active_Material : GameRes.Data.Gfx.command_paper_Material);
			canvas.DrawTexturedRect(screenRect.MinX + 210, screenRect.MinY + 10);
			canvas.CurrentState.SetMaterial(btnScissor ? GameRes.Data.Gfx.command_scissor_active_Material : GameRes.Data.Gfx.command_scissor_Material);
			canvas.DrawTexturedRect(screenRect.MinX + 110, screenRect.MinY + 10);

			canvas.CurrentState.ColorTint = ColorRgba.White;
			canvas.CurrentState.SetMaterial(cmdRock.GetIconMaterial());
			canvas.DrawTexturedRect(screenRect.MinX + 310 - 20, screenRect.MinY + 10 + 70);
			canvas.CurrentState.SetMaterial(cmdPaper.GetIconMaterial());
			canvas.DrawTexturedRect(screenRect.MinX + 210 - 20, screenRect.MinY + 10 + 70);
			canvas.CurrentState.SetMaterial(cmdScissor.GetIconMaterial());
			canvas.DrawTexturedRect(screenRect.MinX + 110 - 20, screenRect.MinY + 10 + 70);

			canvas.PopState();
		}
		public void DrawLemonGUI(Canvas canvas)
		{
            Rect screenRect = new Rect(DualityApp.TargetResolution);
			PlayerIdentity id = PlayerIdentity.Lemon;
			float mana = Player.Lemon.Mana;
			float hp = Player.Lemon.Hq != null ? Player.Lemon.Hq.Hitpoints : 0.0f;
			bool btnRock = Player.Lemon.CommandingRock;
			bool btnPaper = Player.Lemon.CommandingPaper;
			bool btnScissor = Player.Lemon.CommandingScissor;
			WarriorCommand cmdRock = Player.Lemon.CurrentCmdRock;
			WarriorCommand cmdPaper = Player.Lemon.CurrentCmdPaper;
			WarriorCommand cmdScissor = Player.Lemon.CurrentCmdScissor;
			canvas.PushState();

            canvas.CurrentState.TransformAngle = -MathF.RadAngle90;

            canvas.CurrentState.SetMaterial(GameRes.Data.Gfx.emptymana_Material);
            canvas.CurrentState.TransformHandle = new Vector2(75, 200);
			canvas.DrawTexturedRect(screenRect.MaxX, screenRect.MinY);

            canvas.CurrentState.SetMaterial(GameRes.Data.Gfx.fullmana_Material);
            canvas.CurrentState.TransformHandle = new Vector2(75, 200 * mana);
			canvas.DrawTexturedRect(
				screenRect.MaxX, screenRect.MinY, 75, 200 * mana,
				0.0f, 1.0f - mana, 1.0f, mana);

            canvas.CurrentState.TransformHandle = new Vector2(0, 0);
			canvas.CurrentState.ColorTint = ColorRgba.Black;
			canvas.DrawText(
				string.Format("{0}", MathF.RoundToInt(mana * 100.0f)), 
				screenRect.MaxX - 30 + 2, 
				screenRect.MinY + 110 - 2);
			canvas.CurrentState.ColorTint = new ColorRgba(96, 164, 255);
			canvas.DrawText(
				string.Format("{0}", MathF.RoundToInt(mana * 100.0f)), 
				screenRect.MaxX - 30, 
				screenRect.MinY + 110);

            canvas.CurrentState.TransformHandle = new Vector2(0, 0);
			canvas.CurrentState.ColorTint = ColorRgba.Black;
			canvas.DrawText(
				string.Format("{0}", MathF.RoundToInt(hp * 100.0f)), 
				screenRect.MaxX - 30 + 2, 
				screenRect.MinY + 160 - 2);
			canvas.CurrentState.ColorTint = new ColorRgba(255, 64, 64);
			canvas.DrawText(
				string.Format("{0}", MathF.RoundToInt(hp * 100.0f)), 
				screenRect.MaxX - 30, 
				screenRect.MinY + 160);

			canvas.CurrentState.ColorTint = id.GetUnitColor();
			canvas.CurrentState.SetMaterial(btnRock ? GameRes.Data.Gfx.command_rock_active_Material : GameRes.Data.Gfx.command_rock_Material);
			canvas.DrawTexturedRect(screenRect.MaxX - 310, screenRect.MaxY - 10);
			canvas.CurrentState.SetMaterial(btnPaper ? GameRes.Data.Gfx.command_paper_active_Material : GameRes.Data.Gfx.command_paper_Material);
			canvas.DrawTexturedRect(screenRect.MaxX - 210, screenRect.MaxY - 10);
			canvas.CurrentState.SetMaterial(btnScissor ? GameRes.Data.Gfx.command_scissor_active_Material : GameRes.Data.Gfx.command_scissor_Material);
			canvas.DrawTexturedRect(screenRect.MaxX - 110, screenRect.MaxY - 10);

			canvas.CurrentState.ColorTint = ColorRgba.White;
			canvas.CurrentState.SetMaterial(cmdRock.GetIconMaterial());
			canvas.DrawTexturedRect(screenRect.MaxX - 310 + 20, screenRect.MaxY - 10 - 70);
			canvas.CurrentState.SetMaterial(cmdPaper.GetIconMaterial());
			canvas.DrawTexturedRect(screenRect.MaxX - 210 + 20, screenRect.MaxY - 10 - 70);
			canvas.CurrentState.SetMaterial(cmdScissor.GetIconMaterial());
			canvas.DrawTexturedRect(screenRect.MaxX - 110 + 20, screenRect.MaxY - 10 - 70);

			canvas.PopState();
		}
		public void DrawInputPaths(Canvas canvas)
		{
			canvas.PushState();

			canvas.CurrentState.SetMaterial(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White));
			List<TuioInstance> tuioList = TuioListener.Instance.TuioInstances.ToList();
			foreach (TuioInstance instance in tuioList)
			{
				PlayerIdentity pathPlayerId = instance.Path.First().pos.X <= 0.5f ? PlayerIdentity.Cherry : PlayerIdentity.Lemon;
				canvas.CurrentState.ColorTint = pathPlayerId.GetUnitColor(0.0f, 0.35f);
				List<TuioInstance.PathNode> instancePath = instance.Path.ToList();
				for (int i = 0; i < instancePath.Count - 1; i++)
				{
					canvas.DrawLine(
						instancePath[i].pos.X * DualityApp.TargetResolution.X, 
						instancePath[i].pos.Y * DualityApp.TargetResolution.Y,
						instancePath[i + 1].pos.X * DualityApp.TargetResolution.X, 
						instancePath[i + 1].pos.Y * DualityApp.TargetResolution.Y);
				}
				canvas.FillCircle(instance.Pos.X * DualityApp.TargetResolution.X, instance.Pos.Y * DualityApp.TargetResolution.Y, 5.0f);
				canvas.CurrentState.ColorTint = canvas.CurrentState.ColorTint.WithAlpha(0.5f);
				canvas.FillCircle(instance.Pos.X * DualityApp.TargetResolution.X, instance.Pos.Y * DualityApp.TargetResolution.Y, 10.0f);
				canvas.FillCircle(instance.Pos.X * DualityApp.TargetResolution.X, instance.Pos.Y * DualityApp.TargetResolution.Y, 15.0f);
				canvas.CurrentState.ColorTint = canvas.CurrentState.ColorTint.WithAlpha(1.0f);
			}

			canvas.PopState();
		}
	}
}
